<template>
  <el-container class="h-full bg-white">
    <el-header v-if="searchOption" class="border-b-2" style="height: auto">
      <el-form
        :inline="true"
        :model="defaultParms"
        size="default"
        label-position="left"
        class="table_header_form pt-3"
      >
        <template v-for="(option, index) in searchOption" :key="index">
          <template v-if="option.type == 'input'">
            <el-form-item :label="option.label" :prop="option.key">
              <el-input
                v-model="defaultParms[option.key]"
                :placeholder="
                  defaultParms.placeholder ? defaultParms.placeholder : '请输入'
                "
                :style="`width: ${option.width ? option.width : '100px'}`"
              />
            </el-form-item>
          </template>
          <template v-else-if="option.type == 'select'">
            <el-form-item :label="option.label" :prop="option.key">
              <el-select
                v-model="defaultParms[option.key]"
                :placeholder="
                  defaultParms.placeholder ? defaultParms.placeholder : '请选择'
                "
                :style="`width: ${option.width ? option.width : '100px'}`"
              >
                <el-option
                  v-for="(item, itemIndex) in option.searchData"
                  :key="itemIndex"
                  :label="item.code ? `${item.code} - ${item.name}` : item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </template>
        </template>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <div class="p-2">
        <slot name="header_btn"></slot>
      </div>
    </el-header>
    <el-main>
      <el-auto-resizer>
        <template #default="{ height }">
          <el-table
            :data="props.tableData"
            v-bind="attrs"
            :max-height="height"
            @selection-change="handleSelectionChange"
          >
            <template
              v-for="(column, index) in props.columnsList"
              :key="column.prop || index"
            >
              <!-- 操作列 -->
              <el-table-column
                v-if="column.type == 'operation'"
                :align="column.align ? column.align : 'center'"
                v-bind="Object.assign(column, column.itemAttr)"
              >
                <template #default="scope">
                  <el-button
                    v-for="(btn, index) in column.buttons"
                    :key="index"
                    size="small"
                    link
                    @click="
                      () => {
                        btn.done(scope)
                      }
                    "
                  >
                    {{ btn.name }}
                  </el-button>
                </template>
              </el-table-column>
              <!-- 基础表格配置 -->
              <el-table-column
                v-else-if="column.type"
                :align="column.align ? column.align : 'center'"
                v-bind="Object.assign(column, column.itemAttr)"
              >
              </el-table-column>
              <!-- 表格内容 -->
              <el-table-column
                v-else
                v-bind="Object.assign(column, column.itemAttr)"
                :align="column.align ? column.align : 'center'"
              >
                <template #default="scope">
                  <!-- 模板内容 -->
                  <template v-if="column.template">
                    <div v-html="column.template(scope.row)"></div>
                  </template>
                  <!-- 文本 -->
                  <template v-else>
                    <slot :name="column.slotName" :row="scope.row">
                      {{ column.prop && scope.row[column.prop] }}
                    </slot>
                  </template>
                </template>
              </el-table-column>
            </template>
          </el-table>
        </template>
      </el-auto-resizer>
    </el-main>
    <el-footer>
      <el-pagination
        v-model:currentPage="searchParams.page"
        v-model:page-size="searchParams.limit"
        class="float-left"
        :page-sizes="[10, 20, 50, 100, 200]"
        small="small"
        layout="total, sizes, prev, pager, next, jumper"
        :total="searchParams.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <div class="float-right footer-right">
        <slot name="footer_btn"></slot>
      </div>
    </el-footer>
  </el-container>
</template>

<script lang="ts" setup>
import { useVModel } from '@vueuse/core'
import { useAttrs, toRaw } from 'vue'
import type { ITableCol, searchParams } from './../types'
import { ElAutoResizer } from 'element-plus'
import { Search } from '@element-plus/icons-vue'

interface Props {
  tableData?: any[] | undefined
  columnsList?: ITableCol[]
  searchParams?: searchParams
  searchMethod?: any
  multipleSelection?: Array
  searchOption?: Array
}
// 接收ElTable属性，默认绑定
const attrs = useAttrs()
const props = defineProps<Props>()

// 定义响应数据更新父组件的值
const emits = defineEmits(
  ['update:multipleSelection'],
  ['update:searchParams'],
  ['update:searchOption'],
)
const multipleSelection = useVModel(props, 'multipleSelection', emits)
const defaultParms = useVModel(props, 'searchParams', emits)
const searchOption = useVModel(props, 'searchOption', emits)

// 初始化参数
const initSearchData = () => {
  searchOption.value.forEach((item: any) => {
    const itemData = toRaw(item)
    if (itemData.default) {
      defaultParms.value[itemData.key] = itemData.default
    }
  })
}
initSearchData()

// 分页
const handleSizeChange = (d: string) => {
  defaultParms.value.limit = d
  props.searchMethod()
}

const handleCurrentChange = (d: string) => {
  defaultParms.value.page = d
  props.searchMethod()
}

const handleSelectionChange = (d: any) => {
  multipleSelection.value = d
}

const search = () => {
  props.searchMethod()
}
// const update = (event: any) => {
//   multipleSelection.value = event.target.value
// }

// defineExpose({
//   // 可以通过ref的方式的访问
//   update,
// })
</script>
